//
//  ChatLeaveMessageView.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/4/30.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "ChatLeaveMessageView.h"
#import "CommonHeader.h"
#import "UILabel+NLCustomLab.h"


static NSString*nameStr=@"name";
static NSString*placeStr=@"place";
@implementation ChatLeaveInputView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.backgroundColor=[UIColor whiteColor];
        [self setUpView];
    }
    return self;
}
-(void)setUpView{
    UILabel*contentLabel = [UILabel new];
    self.nameLabel=contentLabel;
    contentLabel.text=@"姓名";
    contentLabel.numberOfLines=1;
    contentLabel.font =KIMFontRegularSize(14);
    contentLabel.textColor=[UIColor jk_colorWithHexString:@"#202034"];
    [self addSubview:contentLabel];
    CGFloat leftMargin=adaptWidth750(40);
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(leftMargin);
        make.width.mas_equalTo(adaptWidth750(130));
    }];
    
    UIView*textFieldView =[UIView new];
    [self addSubview:textFieldView];
       [textFieldView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(contentLabel);
           make.left.equalTo(contentLabel.mas_right);
           make.width.mas_equalTo(adaptWidth750(600));
           make.height.mas_equalTo(adaptWidth750(52*2));
       }];
    
    
    UITextField*inputView =[UITextField new];
    self.inputView=inputView;
    inputView.placeholder=@"请输入姓名";
    inputView.font =KIMFontRegularSize(14);
    inputView.textColor=self.nameLabel.textColor;
   
    [textFieldView addSubview:inputView];
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(adaptWidth750(20));
        make.height.equalTo(textFieldView);
        make.centerY.equalTo(contentLabel);
        make.right.mas_equalTo(0);
    }];
    
    
    UIView*lineView = [UIView new];
    self.lineView=lineView;
    lineView.backgroundColor=KLineColor;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(@(1));
    }];
}





-(void)reloadViewWithDic:(NSDictionary*)dic{
    NSString*name =dic[nameStr];
    NSString*place = dic[placeStr];
    self.nameLabel.text=name;
    NSString*redStr=@"*";
//    redStr=[name isEqualToString:@"姓名"]?@"":@"*";
    NSRange range =[name rangeOfString:redStr];
   self.nameLabel= [self.nameLabel initWithAttributedTitle:name andTextColor:[UIColor jk_colorWithHexString:@"#202034"] andTextAlignment:NSTextAlignmentLeft andFont:KIMFontRegularSize(14) andLineSpacing:0 andRange:range andRangeTextColor:[UIColor redColor] andRangeFont:KIMFontRegularSize(14)];
    self.inputView.placeholder=place;
    
}

@end



@implementation ChatLeaveTextView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpView];
    }
    return self;
}

-(void)setUpView{

    CGFloat leftSpace=adaptWidth750(30);
    UITextView*leaveTextView= [UITextView new];
    leaveTextView.font =KIMFontRegularSize(14);
    leaveTextView.delegate=self;
    [self addSubview:leaveTextView];
   
    [leaveTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftSpace);
        make.right.mas_equalTo(-leftSpace);
        make.top.mas_equalTo(adaptWidth750(32));
        make.bottom.mas_equalTo(-adaptWidth750(32));
    }];
    
    
    UILabel*placeLabel = [UILabel new];
    self.placeLabel=placeLabel;
    placeLabel.text=@"请输入留言...";
    placeLabel.font=KIMFontRegularSize(14);
    placeLabel.textColor=[UIColor jk_colorWithHexString:@"#AFB8BB"];
    [leaveTextView addSubview:placeLabel];
    [placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(leaveTextView);
    }];
    
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.placeLabel.hidden=YES;
}

-(void)textViewDidChange:(UITextView *)textView{
    NSString*textStr=textView.text;
    self.placeLabel.hidden=textStr.length>0?YES:NO;
}

@end




@implementation ChatLeaveMessageView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpView];
    }
    return self;
}

-(void)setUpView{
    NSArray*arr=@[@{@"name":@"  姓    名",@"place":@"请输入姓名"},@{@"name":@"* 手机号",@"place":@"请输入手机号"}];
    for (int i=0; i<arr.count; i++) {
        ChatLeaveInputView*inputView =[ChatLeaveInputView new];
        [self addSubview:inputView];
        NSDictionary*dic=arr[i];
        [inputView reloadViewWithDic:dic];
        if (i==1) {
            self.phoneInputView=inputView;
            inputView.lineView.hidden=YES;
            inputView.inputView.keyboardType=UIKeyboardTypeNumberPad;
             [inputView.inputView addTarget:self action:@selector(textStrDidChange:) forControlEvents:UIControlEventEditingChanged];
        }else{
            self.nameInputView=inputView;
        }
        CGFloat inputHeight=adaptWidth750(52*2);
        [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.mas_equalTo(inputHeight*i);
            make.height.mas_equalTo(inputHeight);
        }];
    }
    
    ChatLeaveTextView*textView = [ChatLeaveTextView new];
    textView.backgroundColor  =[UIColor whiteColor];
    self.textView=textView;
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneInputView.mas_bottom).offset(adaptWidth750(20));
        make.left.right.equalTo(self);
        make.height.mas_equalTo(adaptWidth750(104*2));
    }];
    
}

-(void)textStrDidChange:(UITextField*)textField{
    if (textField.text.length>11) {
        textField.text=[textField.text substringToIndex:11];//保证手机号不能大于11位
    }
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
